export function getRootPath() {
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    var localhostPaht = curWwwPath.split('#')[0]


    return (localhostPaht + '#');
};

/**
 * @params data是当前时间的上个月
 *
 */

export function GetPreMonthDay(date) {
    console.log(typeof date)
    var daysInMonth = new Array([0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31]);
    var strYear = date.getFullYear();
    var strDay = date.getDate();
    var strMonth = date.getMonth() + 1;
    if (strYear % 4 == 0 && strYear % 100 != 0) {
        daysInMonth[2] = 29;
    }
    if (strMonth - 1 == 0) {
        strYear -= 1;
        strMonth = 12;
    } else {
        strMonth -= 1;
    }
    strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
    if (strMonth < 10) {
        strMonth = "0" + strMonth;
    }
    if (strDay < 10) {
        strDay = "0" + strDay;
    }
    datastr = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
    return datastr;
}
//昨天
export function getYesterday() {
    var day1 = new Date();
    day1.setDate(day1.getDate() - 1);
    var s1 = formatDate(day1, "yyyy-MM-dd");
    return s1;
}
//上个月的第一天和最后一天

export function getFirstDayAndLastDayByLastMonth() {
    var nowdays = new Date();
    var year = nowdays.getFullYear();
    var month = nowdays.getMonth();
    if (month == 0) {
        month = 12;
        year = year - 1;
    }
    if (month < 10) {
        month = "0" + month;
    }
    var firstDay = year + "-" + month + "-" + "01";
    var myDate = new Date(year, month, 0);
    var lastDay = year + "-" + month + "-" + myDate.getDate();
    return {
        firstDay,
        lastDay
    }
}
/* 获取上一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
export function getPreMonth(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份  
    var month = arr[1]; //获取当前日期的月份  
    var day = arr[2]; //获取当前日期的日  
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数  
    var year2 = year;
    var month2 = parseInt(month) - 1;
    if (month2 == 0) {
        year2 = parseInt(year2) - 1;
        month2 = 12;
    }
    var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
        day2 = days2;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = year2 + '-' + month2 + '-' + day2;
    return t2;
}
/** 
 * 获取下一个月 
 * 
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25 
 */
export function getNextMonth(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份  
    var month = arr[1]; //获取当前日期的月份  
    var day = arr[2]; //获取当前日期的日  
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中的月的天数  
    var year2 = year;
    var month2 = parseInt(month) + 1;
    if (month2 == 13) {
        year2 = parseInt(year2) + 1;
        month2 = 1;
    }
    var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
        day2 = days2;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }

    var t2 = year2 + '-' + month2 + '-' + day2;
    return t2;
}




//时间间隔1个月
export function checkTime(begintime, endtime) {

    //判断时间跨度是否大于3个月
    var arr1 = begintime.split(' ')[0].split('-');
    var arr2 = endtime.split(' ')[0].split('-');

    arr1[1] = parseInt(arr1[1]);
    arr1[2] = parseInt(arr1[2]);
    arr2[1] = parseInt(arr2[1]);
    arr2[2] = parseInt(arr2[2]);
    var flag = true;
    if (arr1[0] == arr2[0]) { //同年
        if (arr2[1] - arr1[1] > 1) { //月间隔超过3个月
            flag = false;
        } else if (arr2[1] - arr1[1] == 1) { //月相隔3个月，比较日
            if (arr2[2] > arr1[2]) { //结束日期的日大于开始日期的日
                flag = false;
            }
        }
    } else { //不同年
        if (arr2[0] - arr1[0] > 1) {
            flag = false;
        } else if (arr2[0] - arr1[0] == 1) {
            if (arr1[1] < 12) { //开始年的月份小于10时，不需要跨年
                flag = false;
            } else if (arr1[1] + 1 - arr2[1] < 12) { //月相隔大于3个月
                flag = false;
            } else if (arr1[1] + 1 - arr2[1] == 12) { //月相隔3个月，比较日
                if (arr2[2] > arr1[2]) { //结束日期的日大于开始日期的日
                    flag = false;
                }
            }
        }
    }
    if (!flag) {

        return false;
    }
    return true;
}

//时间格式化
export function formatDate(date, fmt) {

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
        }
    }
    return fmt
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length)
}
export function format(time, fmt) {
    if (!time) return time;
    let a = time.replace(/\s+/g, 'T');
    let date = new Date(a)
    return formatDate(date, fmt)
}

/**
 * 设置时间段
 * @param {*} datetime 默认查询26-25号
 */
export function setTimeslot(datetime) {
    if (!datetime) datetime = 25;
    var theDay = new Date().getDate();
    var theYear = new Date().getFullYear();
    var theMonth = new Date().getMonth();
    var date = theYear + '-' + (theMonth + 1) + '-' + (datetime + 1)
    var date1 = theYear + '-' + (theMonth + 1) + '-' + datetime
    var begindate = format(date, 'yyyy-MM-dd')
    var enddate = format(date1, 'yyyy-MM-dd')
    if (theDay > datetime) {
        return {
            begindate: begindate,
            enddate: getNextMonth(enddate)
        }
    } else {
        return {
            begindate: getPreMonth(begindate),
            enddate: enddate
        }
    }
}

//指定年月的第一天和最后一天  y = 2012, m = 1

function getAppointDays(y, m) {

    // var firstDay = new Date(y, m - 1, 1);


    var month = m < 10 ? '0' + m : m;

    var firstDay = y + "-" + month + "-" + "01";

    var myDate = new Date(y, m, 0);
    var lastDay = y + "-" + month + "-" + myDate.getDate();
    return {
        bengin: firstDay,
        end: lastDay
    }
}

/**
 * 设置分组图时间段
 * @param {*} datetime 默认26号为界查询1年的数据，
 * 如当前时间2018.12.25 =》 2017.12.01-2018.11.30
 * 当前时间2018.12.26 =》 2018.01.01-2018.12.31
 */

export function setGroupDate(datetime) {
    if (!datetime) datetime = 25;
    var theDay = new Date().getDate();
    var theYear = new Date().getFullYear();
    var theMonth = new Date().getMonth();
    var y0, y1, m0, m1;
    // var date = theYear + '-' + (theMonth + 1) + '-' + (datetime + 1)
    // var date1 = theYear + '-' + (theMonth + 1) + '-' + datetime
    // var begindate = format(date, 'yyyy-MM-dd')
    // var enddate = format(date1, 'yyyy-MM-dd')

    if (theDay > datetime) { //>26
        m1 = theMonth + 1;
        y1 = theYear;
        if (m1 - 12 == 0) {
            m0 = 1;
            y0 = theYear;
        } else {
            m0 = m1;
            y0 = theYear - 1;
        }

    } else {

        if (theMonth > 0) {
            m1 = theMonth;
            y1 = theYear;
            m0 = m1 + 1;
            y0 = theYear - 1;

        } else {
            m1 = 12;
            y1 = theYear - 1;
            m0 = 1;
            y0 = y1;
        }




    }
    return {
        begindate: getAppointDays(y0, m0).bengin,
        enddate: getAppointDays(y1, m1).end
    }
}

/**
 *
 * @param {*} datetime 指定时间向前推一年 datetime："2019-01-11"
 */
export function forwardYearByTheDay(datetime) {
    if (!datetime) datetime = formatDate(new Date(), 'yyyy-MM-dd');
    // var theDay = new Date(datetime).getDate();
    var theYear = new Date(datetime).getFullYear();
    var theMonth = new Date(datetime).getMonth() + 1;

    var month = theMonth < 10 ? '0' + theMonth : theMonth;

    var forward = (theYear - 1) + "-" + month + "-" + "01";
    return forward
}





/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}


export function MyClass() {}

MyClass.hasClassName = function (element, className) {
    if (!element) return;
    var elementClassName = element.className;
    if (elementClassName.length == 0) return false;
    //用正则表达式判断多个class之间是否存在真正的class（前后空格的处理）
    if (elementClassName == className || elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
        return true;
    return false;
};

MyClass.addClassName = function (element, className) {
    if (!element) return;
    var elementClassName = element.className;
    if (elementClassName.length == 0) {
        element.className = elementClassName;
        return;
    }
    if (elementClassName == className || elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
        return;
    element.className = elementClassName + " " + className;
};

MyClass.removeClassName = function (element, className) {
    if (!element) return;
    var elementClassName = element.className;
    if (elementClassName.length == 0) return;
    if (elementClassName == className) {
        element.className = "";
        return;
    }
    if (elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
        element.className = elementClassName.replace((new RegExp("(^|\\s)" + className + "(\\s|$)")), " ");
};